// PaintDialog.cpp : implementation file
//

#include "stdafx.h"
#include "../include/UMyChart9.h"
#include "PaintDialog.h"
#include <afxcmn.h>

#ifdef _DEBUG
	#undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
	#define new DEBUG_NEW
#endif

DWORD __CHECK_BUTTON_ID_LIST__[]=
{
	ID_BTN_LINE,
	ID_BTN_STRAIGHT_LINE,
	ID_BTN_ARRAW,
	ID_BTN_HALF_LINE,
	ID_BTN_GOLDEN_SECTION,
	ID_BTN_DOWN,
	ID_BTN_UP,
	ID_BTN_D_STRAIGHT_LINE,
	ID_BTN_DEMI_CERCLE,
	ID_BTN_RECT,
	ID_BTN_JIAN_EN_ANGLE_LINE,
	ID_BTN_ZU_SU_LINE,
	ID_BTN_PERIOD,
	ID_BTN_FEIBOLA_LINE,
	ID_BTN_WAVE_LINE,
	ID_BTN_PERCENT_LINE,
	ID_BTN_TEXT,
};

enum __PAINT_TOOL_BUTTON_ID_INDEX_
{
	BTN_INDEX_NONE=0,
	BTN_INDEX_HIDE,
	BTN_INDEX_LINE,
	BTN_INDEX_STRAIGHT_LINE,
	BTN_INDEX_ARRAW,
	BTN_INDEX_HALF_LINE,
	BTN_INDEX_GOLDEN_SECTION,
	BTN_INDEX_UP,
	BTN_INDEX_DOWN,
	BTN_INDEX_D_STRAIGHT_LINE,
	BTN_INDEX_DEMI_CERCLE,
	BTN_INDEX_RECT,
	BTN_INDEX_JIAN_EN_ANGLE_LINE,
	BTN_INDEX_ZU_SU_LINE,
	BTN_INDEX_PERIOD,
	BTN_INDEX_FEIBOLA_LINE,
	BTN_INDEX_WAVE_LINE,
	BTN_INDEX_PERCENT_LINE,
	BTN_INDEX_TEXT,
	BTN_INDEX_MAX,
};

DWORD __CHECK_BUTTON_ID_INDEX_LIST__[]=
{
	BTN_INDEX_HIDE,
	BTN_INDEX_LINE,
	BTN_INDEX_STRAIGHT_LINE,
	BTN_INDEX_ARRAW,
	BTN_INDEX_HALF_LINE,
	BTN_INDEX_GOLDEN_SECTION,
	BTN_INDEX_DOWN,
	BTN_INDEX_UP,
	BTN_INDEX_D_STRAIGHT_LINE,
	BTN_INDEX_DEMI_CERCLE,
	BTN_INDEX_RECT,
	BTN_INDEX_JIAN_EN_ANGLE_LINE,
	BTN_INDEX_ZU_SU_LINE,
	BTN_INDEX_PERIOD,
	BTN_INDEX_FEIBOLA_LINE,
	BTN_INDEX_WAVE_LINE,
	BTN_INDEX_PERCENT_LINE,
	BTN_INDEX_TEXT,
};

int __CHECK_BUTTON_COUNT__=sizeof(__CHECK_BUTTON_ID_LIST__)/sizeof(__CHECK_BUTTON_ID_LIST__[0]);
int __CHECK_BUTTON_INDEX_COUNT__=sizeof(__CHECK_BUTTON_ID_INDEX_LIST__)/sizeof(__CHECK_BUTTON_ID_INDEX_LIST__[0]);
// PaintDialog dialog

IMPLEMENT_DYNAMIC(PaintDialog, CDialog)

PaintDialog::PaintDialog(CWnd* pParent /*=NULL*/)
	: CDialog(PaintDialog::IDD, pParent),m_bStartPaint(FALSE),
	m_bHideAll(FALSE)
{

}

PaintDialog::~PaintDialog()
{
}

void PaintDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(PaintDialog, CDialog)
	ON_BN_CLICKED(ID_BTN_NONE, &PaintDialog::OnBnClickedBtnNone)
	ON_BN_CLICKED(ID_BTN_LINE, &PaintDialog::OnBnClickedBtnLine)
	ON_BN_CLICKED(ID_BTN_STRAIGHT_LINE,&PaintDialog::OnBnClickedBtnSLine)
	ON_BN_CLICKED(ID_BTN_HALF_LINE,&PaintDialog::OnBnClickedBtnHalfLine)
	ON_BN_CLICKED(ID_BTN_ARRAW,&PaintDialog::OnBnClickedBtnArraw)
	ON_BN_CLICKED(ID_BTN_GOLDEN_SECTION,&PaintDialog::OnBnClickedGoldenSection)
	ON_BN_CLICKED(ID_BTN_HIDE,&PaintDialog::OnBnClickedHide)
	ON_BN_CLICKED(ID_BTN_UP,&PaintDialog::OnBnClickedUp)
	ON_BN_CLICKED(ID_BTN_DOWN,&PaintDialog::OnBnClickedDown)
	ON_BN_CLICKED(ID_BTN_D_STRAIGHT_LINE,&PaintDialog::OnBnClickedBtnDSLine)
	ON_BN_CLICKED(ID_BTN_DEMI_CERCLE,&PaintDialog::OnBnClickedBtnSemicircle)
	ON_BN_CLICKED(ID_BTN_RECT,&PaintDialog::OnBnClickedRect)
	ON_BN_CLICKED(ID_BTN_JIAN_EN_ANGLE_LINE,&PaintDialog::OnBnClickedJangEnAngleLine)
	ON_BN_CLICKED(ID_BTN_ZU_SU_LINE,&PaintDialog::OnBnClickedZuSuLine)
	ON_BN_CLICKED(ID_BTN_PERIOD,&PaintDialog::OnBnClickedPeriodLine)
	ON_BN_CLICKED(ID_BTN_FEIBOLA_LINE,&PaintDialog::OnBnClickedFeiBoLaLine)
	ON_BN_CLICKED(ID_BTN_WAVE_LINE,&PaintDialog::OnBnClickedWaveLine)
	ON_BN_CLICKED(ID_BTN_PERCENT_LINE,&PaintDialog::OnBnClickedPercentLine)
	ON_BN_CLICKED(ID_BTN_TEXT,&PaintDialog::OnBnClickedText)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// PaintDialog message handlers


BOOL PaintDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	if (!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR_PAINT))
	{
		return -1;
	}

	int nCount=m_wndToolBar.GetCount();
	int nRow=nCount/4;

	if (nCount%4 != 0) ++nRow;

	for(int i=0;i<__CHECK_BUTTON_INDEX_COUNT__;++i)
	{
		m_wndToolBar.SetButtonStyle(__CHECK_BUTTON_ID_INDEX_LIST__[i],TBBS_CHECKBOX);
	}

	
	RECT rt={0};
	m_wndToolBar.SetBarStyle(TBSTYLE_WRAPABLE|CBRS_SIZE_FIXED|CBRS_ALIGN_TOP|CBRS_FLOATING);
	//m_wndToolBar.SetBarStyle(TBSTYLE_WRAPABLE|CBRS_SIZE_FIXED|CBRS_TOP|CBRS_FLOATING);
	m_wndToolBar.GetToolBarCtrl().SetRows(nRow,TRUE,&rt);
	//m_wndToolBar.SetBarStyle(CBRS_ALIGN_TOP|CBRS_FLOATING);
	

	RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);

	if (m_ToolTip.Create(this))
	{
		for(int i=0;i<nCount;++i)
		{
			CRect rtButton;
			TBBUTTON btn={0};

			if ( m_wndToolBar.GetToolBarCtrl().GetButton(i, &btn) && 
				( btn.fsStyle == TBSTYLE_BUTTON || btn.fsStyle == TBSTYLE_CHECK) )
			{
				m_wndToolBar.GetItemRect(i,&rtButton);
				m_ToolTip.AddTool(&m_wndToolBar,btn.idCommand,&rtButton,btn.idCommand);
			}

		}
		m_wndToolBar.GetToolBarCtrl().SetToolTips(&m_ToolTip);
	}

	return TRUE;
}


void PaintDialog::OnBnClickedBtnNone()
{
	m_bStartPaint=FALSE;
	ResetCheckButton();
}

void PaintDialog::OnBnClickedBtnLine()
{
	OnPaintButtonChecked(BTN_INDEX_LINE,__paint_line__);	
	ResetCheckButton(ID_BTN_LINE);
}

void PaintDialog::OnBnClickedBtnSLine()
{
	OnPaintButtonChecked(BTN_INDEX_STRAIGHT_LINE,__paint_stright_line__);
	ResetCheckButton(ID_BTN_STRAIGHT_LINE);
}

void PaintDialog::OnBnClickedBtnArraw()
{
	OnPaintButtonChecked(BTN_INDEX_ARRAW,__paint_arraw_line__);
	ResetCheckButton(ID_BTN_ARRAW);
}

void PaintDialog::OnBnClickedBtnHalfLine()
{
	OnPaintButtonChecked(BTN_INDEX_HALF_LINE,__paint_half_line__);
	ResetCheckButton(ID_BTN_HALF_LINE);
}

void PaintDialog::OnBnClickedGoldenSection()
{
	OnPaintButtonChecked(BTN_INDEX_GOLDEN_SECTION,__paint_golden_section__);
	ResetCheckButton(ID_BTN_GOLDEN_SECTION);
}


void PaintDialog::OnBnClickedUp()
{
	OnPaintButtonChecked(BTN_INDEX_UP,__paint_up__);
	ResetCheckButton(ID_BTN_UP);
}

void PaintDialog::OnBnClickedDown()
{
	OnPaintButtonChecked(BTN_INDEX_DOWN,__paint_down__);
	ResetCheckButton(ID_BTN_DOWN);
}

void PaintDialog::OnBnClickedBtnDSLine()
{
	OnPaintButtonChecked(BTN_INDEX_D_STRAIGHT_LINE,__paint_d_stright_line__);
	ResetCheckButton(ID_BTN_D_STRAIGHT_LINE);
}

BOOL PaintDialog:: OnPaintButtonChecked(int nIndex,int nPaintType)
{
	TBBUTTON btnInfo={0};

	if (!m_wndToolBar.GetToolBarCtrl().GetButton(nIndex,&btnInfo)) 
		return FALSE;

	if ( int(btnInfo.fsState & TBSTATE_CHECKED) == TBSTATE_CHECKED)
	{
		m_bStartPaint=TRUE;
		m_nPaintType=nPaintType;
	}
	else
	{
		m_bStartPaint=FALSE;
	}

	return TRUE;
}

void PaintDialog::ResetCheckButton(int nException)
{
	for(int i=0;i<__CHECK_BUTTON_COUNT__;++i)
	{
		if (__CHECK_BUTTON_ID_LIST__[i]==nException) continue;

		if (m_wndToolBar.GetToolBarCtrl().IsButtonChecked(__CHECK_BUTTON_ID_LIST__[i]))
			m_wndToolBar.GetToolBarCtrl().CheckButton(__CHECK_BUTTON_ID_LIST__[i],FALSE);
	}	
}

void PaintDialog::OnBnClickedBtnSemicircle()
{
	OnPaintButtonChecked(BTN_INDEX_DEMI_CERCLE,__paint_semi_circle__);
	ResetCheckButton(ID_BTN_DEMI_CERCLE);
}

void PaintDialog::OnBnClickedRect()
{
	OnPaintButtonChecked(BTN_INDEX_RECT,__paint_rect__);
	ResetCheckButton(ID_BTN_RECT);
}

void PaintDialog::OnBnClickedJangEnAngleLine()
{
	OnPaintButtonChecked(BTN_INDEX_JIAN_EN_ANGLE_LINE,__paint_jiang_en_angle_line__);
	ResetCheckButton(ID_BTN_JIAN_EN_ANGLE_LINE);
}

void PaintDialog::OnBnClickedZuSuLine()
{
	OnPaintButtonChecked(BTN_INDEX_ZU_SU_LINE,__paint_zu_su_line__);
	ResetCheckButton(ID_BTN_ZU_SU_LINE);
}

void PaintDialog::OnBnClickedPeriodLine()
{
	OnPaintButtonChecked(BTN_INDEX_PERIOD,__paint_period_line__);
	ResetCheckButton(ID_BTN_PERIOD);
}

void PaintDialog::OnBnClickedFeiBoLaLine()
{
	OnPaintButtonChecked(BTN_INDEX_FEIBOLA_LINE,__paint_feibola_line__);
	ResetCheckButton(ID_BTN_FEIBOLA_LINE);	
}

void PaintDialog::OnBnClickedWaveLine()
{
	OnPaintButtonChecked(BTN_INDEX_WAVE_LINE,__paint_wave_line__);
	ResetCheckButton(ID_BTN_WAVE_LINE);	
}

void PaintDialog::OnBnClickedPercentLine()
{
	OnPaintButtonChecked(BTN_INDEX_PERCENT_LINE,__paint_percent_line__);
	ResetCheckButton(ID_BTN_PERCENT_LINE);	
}

void PaintDialog::OnBnClickedText()
{
	OnPaintButtonChecked(BTN_INDEX_TEXT,__paint_text__);
	ResetCheckButton(ID_BTN_TEXT);
}


void PaintDialog::OnBnClickedHide()
{
	int nCount=m_wndToolBar.GetCount();
	m_bStartPaint=FALSE;
	m_nPaintType=-1;
	
	TBBUTTON btnInfo={0};

	if (!m_wndToolBar.GetToolBarCtrl().GetButton(BTN_INDEX_HIDE,&btnInfo))  return;

	if ( int(btnInfo.fsState & TBSTATE_CHECKED) == TBSTATE_CHECKED)
		m_bHideAll=TRUE;
	else
		m_bHideAll=FALSE;

	ResetCheckButton(ID_BUT_HIDE);
	redraw_all_window(__wnd_mainframe__);
}

void PaintDialog::OnClose()
{
	ShowWindow(SW_HIDE);
	//CDialog::OnClose();
}
